package com.jiyi.dao;

import java.util.ArrayList;
import java.util.List;

import com.jiyi.db.UserDB;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

/**
 * 用户的增删改查*/
public class UserDao {
	private Context context;
	private UserDB mUserDB;

	public UserDao(Context context) {
		this.context = context;
		mUserDB = new UserDB(context);
	}

	//添加
	public void add(String user, String password) {		
		SQLiteDatabase database = mUserDB.getWritableDatabase();		
		ContentValues values = new ContentValues();
		values.put(UserDB.USERACCOUNT, user);
		values.put(UserDB.PASSWORD, password);		
		database.insert(UserDB.USERTB, null, values);		
		database.close();

	}

	//更新
	public void upDate(String user, String password) {
		delete(user);
		add(user, password);
	}

	//删除
	public boolean delete(String user) {		
		SQLiteDatabase database = mUserDB.getWritableDatabase();	
		int count = database.delete(UserDB.USERTB, UserDB.USERACCOUNT + "=?",new String[] { user });
		database.close();
		if (count > 0) {
			return true;
		} else {
			return false;
		}
	}
	
		
	//查找
	public Boolean find(String account,String password) {
		
		List<String> data = new ArrayList<String>();
		SQLiteDatabase database = mUserDB.getReadableDatabase();
		String sql = "select * from usertb where useraccount=? and password=?";
		Cursor cursor = database.rawQuery(sql, new String[]{account,password});
		
		/*while (cursor.moveToNext()) {			
		data.add(cursor.getString(0));
		data.add(cursor.getString(1));			
		}
		cursor.close();
		return data;*/
		
		if(cursor.moveToNext()){
			return true;
		}else{
			return false;
		}
		
	}
	
}
